﻿using System;
using System.Collections.Generic;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using LiSAClient.Core;
using LiSAClient.Core.Interfaces;
using LiSAClient.Core.Messages;
using LiSAClient.Core.Models;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects
{
    class NewProject_Step2ViewModel : ContentViewModel, ICommitable
    {
        public ICommand Commit { get; private set; }

        public ICommand NextStep { get; private set; }

        public ICommand PreviousStep { get; private set; }

        public ICommand AddCriteria { get; private set; }
        
        private Project project;
        public Project Project
        {
            get
            {
                return this.project;
            }

            set
            {
                this.Set(() => this.Project, ref this.project, value);
            }
        }

        private ProjectCriteria newCriteria;
        public ProjectCriteria NewCriteria
        {
            get
            {
                return this.newCriteria;
            }

            set
            {
                this.Set(() => this.NewCriteria, ref this.newCriteria, value);
            }
        }

        private ProjectCriteria selectedCriteria;
        public ProjectCriteria SelectedCriteria
        {
            get
            {
                return this.selectedCriteria;
            }

            set
            {
                this.Set(() => this.SelectedCriteria, ref this.selectedCriteria, value);
            }
        }

        #region Initializers

        public NewProject_Step2ViewModel(Project proj)
        {
            this.Project = proj;
            this.NewCriteria = new ProjectCriteria();
            this.Commit = new RelayCommand(SaveProject);
            this.NextStep = new RelayCommand(DoNextStep);
            this.PreviousStep = new RelayCommand(DoPreviousStep);
            this.AddCriteria = new RelayCommand(DoAddCriteria);
        }
        #endregion

        #region methods
        /// <summary>
        /// Move to the enxt step in the wizard
        /// </summary>
        public void DoNextStep()
        {
            this.Presenter.PresentContent(new NewProject_Step3ViewModel(this.Project));
        }
        /// <summary>
        /// Move to the previous step in the wizard
        /// </summary>
        public void DoPreviousStep()
        {
            this.Presenter.PresentContent(new NewProject_Step1ViewModel(this.Project));
        }

        public void DoAddCriteria()
        {
            if (string.IsNullOrWhiteSpace(NewCriteria.CriteriaID) || string.IsNullOrWhiteSpace(NewCriteria.Description))
            {
                return;
            }
            this.NewCriteria.ID = Guid.NewGuid();
            var newCriteriaList = new List<ProjectCriteria>(this.Project.Criteria) {NewCriteria}; 
            this.Project.Criteria = newCriteriaList;
            this.NewCriteria = new ProjectCriteria {CriteriaID = string.Empty, Description = string.Empty};
        }

        /// <summary>
        /// Save the project and close the window
        /// </summary>
        public void SaveProject()
        {
            // Ensure legality
            if (string.IsNullOrWhiteSpace(this.Project.Title))
            {
                Messenger.Default.Send(new ShowNotificationMessage
                {
                    NotificationType = NotificationType.Warning,
                    Header = "Warning!",
                    Body = "The project must have a title."
                });
                return;
            }
            if (string.IsNullOrWhiteSpace(this.Project.Description))
            {
                Messenger.Default.Send(new ShowNotificationMessage
                {
                    NotificationType = NotificationType.Warning,
                    Header = "Warning!",
                    Body = "The project must have a description."
                });
                return;
            }

            this.Project.ProjectLead = AppContext.CurrentContext.User.ID;
            this.Project.CurrentPhase = 0;

            ServiceFactory.Instance.Get<ProjectService>().CreateNewProject(this.Project);

            // Close the window
            if (this.Presenter != null)
            {
                this.Presenter.SendClosingRequest();
            }
        }
        #endregion
    }
}
